<template>
  <!-- 最外层的容器 -->
  <view class="my-settle-container">
    <!-- 全选区域 -->
    <label class="radio" @click="checkedAll">
      <radio color="#C00000" :checked="checkedTotle" /><text>全选</text>
    </label>

    <!-- 合计区域 -->
    <view class="amount-box">
      合计:<text class="amount">￥{{ totalMoney }}</text>
    </view>

    <!-- 结算按钮 -->
    <view class="btn-settle" @click="submitOrder"
      >结算({{ checkedCount }})</view
    >
  </view>
</template>

<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState("goods", ["cart"]),
    ...mapState("address", ["addressList"]),
    ...mapState(["token"]),
    // 总件数
    checkedCount() {
      return this.cart
        .filter((item) => item.goods_state)
        .reduce((totel, num) => (totel += num.goods_count), 0);
    },
    // 全选
    checkedTotle() {
      if (this.cart.filter((i) => i.goods_state).length === this.cart.length) {
        return true;
      } else {
        return false;
      }
    },
    // 计算总价
    totalMoney() {
      return this.cart
        .filter((i) => i.goods_state)
        .reduce(
          (total, item) => (total += item.goods_count * item.goods_price),
          0
        )
        .toFixed(2);
    },
  },
  methods: {
    checkedAll() {
      const res = !this.checkedTotle;
      this.cart.forEach((item) => {
        item.goods_state = res;
      });
    },
    // 提交订单
    submitOrder() {
      if (this.addressList.length === 0) {
        uni.showToast({
          title: "请选择地址",
          icon: "none",
        });
      }
      if (!this.checkedCount) {
        uni.showToast({
          title: "请选择商品",
          icon: "none",
        });
      }
      if (!this.token) {
        uni.showToast({
          title: "请登录",
          icon: "none",
        });
      }
    },
  },
};
</script>

<style lang="scss">
.my-settle-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  // 将背景色从 cyan 改为 white
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 5px;
  font-size: 14px;

  .radio {
    display: flex;
    align-items: center;
  }

  .amount {
    color: #eb4450;
  }

  .btn-settle {
    height: 50px;
    min-width: 100px;
    background-color: #eb4450;
    color: white;
    line-height: 50px;
    text-align: center;
    padding: 0 10px;
  }
}
</style>